package org.example;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.logging.Logger;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.awt.Window;
import java.awt.Frame;

public class EnumMappingDialog extends JDialog {
    private static final Log log = LogFactory.getLog(EnumMappingDialog.class);
    
    private Map<String, String> enumMappings = new HashMap<>();
    private JPanel mappingsPanel;
    private boolean confirmed = false;

    public EnumMappingDialog(JFrame parent, Map<String, String> initialMappings) {
        super(parent, "枚举值映射配置", true);
        if (initialMappings != null) {
            this.enumMappings = new HashMap<>(initialMappings);
        }
        initUI();
    }
    
    public EnumMappingDialog(Window parent, String title, boolean modal, MappingRule rule) {
        super(parent != null ? (Frame) parent : new JFrame(), title, modal);
        log.info("初始化枚举映射对话框，标题：" + title);
        initUI(rule);
    }
    
    private void initUI() {
        setSize(600, 400);
        setLocationRelativeTo(getParent());
        setLayout(new BorderLayout(5, 5));
        
        // 映射条目面板
        mappingsPanel = new JPanel();
        mappingsPanel.setLayout(new BoxLayout(mappingsPanel, BoxLayout.Y_AXIS));
        JScrollPane scrollPane = new JScrollPane(mappingsPanel);
        
        // 添加默认条目
        addMappingEntry("源值", "目标值");
        
        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        JButton addButton = new JButton("添加映射");
        JButton confirmButton = new JButton("确认");
        JButton cancelButton = new JButton("取消");
        
        addButton.addActionListener(this::addMappingEntry);
        confirmButton.addActionListener(e -> {
            confirmed = true;
            dispose();
        });
        cancelButton.addActionListener(e -> dispose());
        
        buttonPanel.add(addButton);
        buttonPanel.add(confirmButton);
        buttonPanel.add(cancelButton);
        
        add(scrollPane, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }
    
    private void initUI(MappingRule rule) {
        setSize(600, 400);
        setLocationRelativeTo(getParent());
        setLayout(new BorderLayout(5, 5));
        
        // 映射条目面板
        mappingsPanel = new JPanel();
        mappingsPanel.setLayout(new BoxLayout(mappingsPanel, BoxLayout.Y_AXIS));
        JScrollPane scrollPane = new JScrollPane(mappingsPanel);
        
        // 添加默认条目
        addMappingEntry("源值", "目标值");
        
        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        JButton addButton = new JButton("添加映射");
        JButton saveButton = new JButton("保存");
        JButton cancelButton = new JButton("取消");
        
        addButton.addActionListener(this::addMappingEntry);
        saveButton.addActionListener(this::saveAction);
        cancelButton.addActionListener(e -> dispose());
        
        buttonPanel.add(addButton);
        buttonPanel.add(saveButton);
        buttonPanel.add(cancelButton);
        
        add(scrollPane, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }
    
    private void saveAction(ActionEvent e) {
        log.debug("保存枚举映射配置");
        enumMappings = getEnumMappings();
        confirmed = true;
        dispose();
        log.info("枚举映射配置保存成功");
    }

    private void addMappingEntry(ActionEvent e) {
        addMappingEntry("", "");
    }

    private void addMappingEntry(String sourceValue, String targetValue) {
        JPanel entryPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
        JTextField sourceField = new JTextField(sourceValue, 15);
        JTextField targetField = new JTextField(targetValue, 15);
        JButton deleteButton = new JButton("删除");
        
        deleteButton.addActionListener(evt -> {
            mappingsPanel.remove(entryPanel);
            mappingsPanel.revalidate();
            mappingsPanel.repaint();
        });
        
        entryPanel.add(new JLabel("源值:"));
        entryPanel.add(sourceField);
        entryPanel.add(new JLabel("目标值:"));
        entryPanel.add(targetField);
        entryPanel.add(deleteButton);
        
        mappingsPanel.add(entryPanel);
        mappingsPanel.revalidate();
        mappingsPanel.repaint();
    }

    public Map<String, String> getEnumMappings() {
        Map<String, String> result = new HashMap<>();
        for (Component comp : mappingsPanel.getComponents()) {
            if (comp instanceof JPanel) {
                JPanel entryPanel = (JPanel) comp;
                Component[] components = entryPanel.getComponents();
                if (components.length >= 5 && 
                    components[1] instanceof JTextField && 
                    components[3] instanceof JTextField) {
                    
                    String source = ((JTextField) components[1]).getText().trim();
                    String target = ((JTextField) components[3]).getText().trim();
                    
                    if (!source.isEmpty() && !target.isEmpty()) {
                        result.put(source, target);
                    }
                }
            }
        }
        return result;
    }

    public boolean isConfirmed() {
        return confirmed;
    }
}